package com.changwu.cwbase.AppTest.DesignPattern;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by changwu on 2018/10/14.
 */
public class PorxyDesign {
    public interface ILawsuit {

        /**
         * 提交申请
         */
        void submit();

        /**
         * 进行举证
         */
        void burden();

        /**
         * 开始辩护
         */
        void defend();

        /**
         * 诉讼完成
         */
        void finish();
    }

    public class XiaoMin implements ILawsuit{

        @Override
        public void submit() {
            //小民申请仲裁
            System.out.println("老板年底拖欠工资，特此申请仲裁！");
        }

        @Override
        public void burden() {
            //小民提交证据
            System.out.println("这是合同书和过去一年的银行工资流水！");
        }

        @Override
        public void defend() {
            //铁证如山
            System.out.println("证据确凿，不需要再说什么！");
        }

        @Override
        public void finish() {
            //结果
            System.out.println("诉讼成功，判决老板即日起七天内结算工资！");
        }

    }

    public class Lawyer implements ILawsuit{

        private ILawsuit mLawsuit; //持有一个具体被代理者的引用

        public Lawyer(ILawsuit lawsuit) {
            this.mLawsuit = lawsuit;
        }

        @Override
        public void submit() {
            mLawsuit.submit();
        }

        @Override
        public void burden() {
            mLawsuit.burden();
        }

        @Override
        public void defend() {
            mLawsuit.defend();
        }

        @Override
        public void finish() {
            mLawsuit.finish();
        }

    }

    public class DynamicPorxy implements InvocationHandler {

        private Object obj; //被代理类的引用

        public DynamicPorxy(Object obj) {
            this.obj = obj;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            // 调用被代理类对象的方法
            Object result = method.invoke(obj, args);
            return result;
        }

    }

    public  void testPorxyDesign(){
//构造出诉讼人小民
        ILawsuit xiaomin = new XiaoMin();

        //构造一个代理律师，并将小民传递进去
        ILawsuit lawyer = new Lawyer(xiaomin);

        //律师提交申请
        lawyer.submit();

        //律师进行举证
        lawyer.burden();

        //律师代小民辩护
        lawyer.defend();

        //完成诉讼
        lawyer.finish();
    }

    public  void testDynamicPorxyDesign(){
        //构造出诉讼人小民
        ILawsuit xiaomin = new XiaoMin();

        //1.静态代理
        //构造一个代理律师，并将小民传递进去
        //ILawsuit lawyer = new Lawyer(xiaomin);

        //--------------------------------------
        //2.动态代理
        //构造一个动态代理
        DynamicPorxy proxy = new DynamicPorxy(xiaomin);

        //获取被代理类小民的ClassLoader
        ClassLoader loader = xiaomin.getClass().getClassLoader();

        //动态构造一个代理者律师
        ILawsuit lawyer = (ILawsuit) Proxy.newProxyInstance(loader, new Class[]{ ILawsuit.class }, proxy);

        //律师提交申请
        lawyer.submit();

        //律师进行举证
        lawyer.burden();

        //律师代小民辩护
        lawyer.defend();

        //完成诉讼
        lawyer.finish();
    }
}
